//
//  AIDiaryReplayBean.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/3/11.
//
/**
 * ai日记 - 豆包
 */
import Foundation
import SwiftUI
class AIDiaryBean : Identifiable ,Codable {
    
    var max_tokens: Int64 = configManager.getConfig().AiConfig.maxTokens ?? 1024
    var temperature: Float64 = configManager.getConfig().AiConfig.temperature ?? 0.9
    var top_p: Float64 = configManager.getConfig().AiConfig.top_p ?? 0.95
    var stream = false
    var messages: [MessagesV2]
    var model :String
    
    
    init(model: String?, list : [DiaryBean], time: TimeInterval, botRoleBean: RoleBean) {
        self.model = model ?? ""
        let userRoleBean = RoleBean.get(userManager.user, botRoleBean)
        self.messages = AIDiaryBean.getMessagesList(list: list, time: time, botRoleBean: botRoleBean, userRoleBean: userRoleBean)
    }
    
    private static func getMessagesList(list : [DiaryBean], time: TimeInterval, botRoleBean: RoleBean,
                                        userRoleBean: RoleBean) ->[MessagesV2] {
        //动作描述
        let headDesc = configManager.getConfig().getAiDescribe().headDescribe ?? ""
        let botDesc =  RoleBean.getBotDesc(botRoleBean)
        let userDesc =  RoleBean.getUserDesc(botRoleBean)
        let memoryDesc = AppStorageManager.shared.toggleMemoryInject ? configManager.getConfig().getAiDescribe().aiMemoryInjectDesc?.formatAiChatContent(ActiveAIMemoryExt.instance.getAiMemoryStr(botRoleBean: botRoleBean)) ?? "" : ""

        let ruleDesc = "\(headDesc)\(configManager.getConfig().getAiDescribe().aiDiaryDescribe ?? "")"
        let enDesc = configManager.getConfig().getAiDescribe().enDesc ?? ""
        let desc = "\(botDesc)\n\n---\n\(userDesc)\(memoryDesc)\(enDesc)"

        var listMsg : [MessagesV2] = []
        listMsg.append(MessagesV2("system", botRoleBean.name ?? "", desc.formatAiChat(botRoleBean,time)))
        
        listMsg.append(MessagesV2("user", userRoleBean.name ?? "", ruleDesc.formatAiChat(botRoleBean,time)))

        return listMsg
    }
    
    
}
